package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.UUID;

/**
 * 通用接口
 */
@RequestMapping("/admin/common")
@RestController
@Slf4j
public class CommonController {

    private static String FILE_UPLOAD_PATH = "D:\\JavaTest\\cangqiong\\sky-take-out\\sky-server\\src\\main\\resources\\upload\\";

    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ResponseBody
    public Result upload(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()){
            return Result.error("文件不能为空");
        }

        File dir = new File(FILE_UPLOAD_PATH);
        if (!dir.exists() || !dir.isDirectory()){
            boolean created = dir.mkdirs();
            if (created){
                log.info("创建文件夹成功：{}",FILE_UPLOAD_PATH);
            }else {
                log.warn("创建文件夹失败或已经存在：{}",FILE_UPLOAD_PATH);
            }
        }

        String originalFilename = file.getOriginalFilename();
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));

        //拼接新的随机名称
        originalFilename = UUID.randomUUID().toString() + extension;

        //确保文件路径安全，避免路径遍历攻击
        Path targetLocation = Paths.get(FILE_UPLOAD_PATH).resolve(originalFilename).normalize();
        try {
            Files.copy(file.getInputStream(),targetLocation, StandardCopyOption.REPLACE_EXISTING);
            log.info("文件上传成功：{}",originalFilename);
        } catch (IOException e) {
            log.error("文件上传失败：{}",originalFilename,e);
            return Result.error(MessageConstant.UPLOAD_FAILED);
        }

        String fileUrl = "http://localhost:8080/static/" + originalFilename;
        return Result.success(fileUrl);

    }
}
